package com.lottery.material.controller.portal.version;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.config.AppVersionControlService;
import com.lottery.material.util.ExceptionUtil;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import com.lottery.material.vo.portal.config.AppVersionControlVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:    APP版本控制
 * Author:         sunny
 * CreateDate:     2019-05-15 13:55
 */
@RequestMapping("/portal/sys")
@Slf4j
@Controller
public class AppVersionControlController extends BaseController {

    @Autowired
    AppVersionControlService appVersionControlService;

    /**
     * description: 获取版本列表信息
     * author sunny
     * createDate 2019-05-15 下午 2:30
     * @param request
     * @return
     */
    @RequestMapping(value = "/getVersion", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getVersion(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if (null != responseResult) {
            return responseResult;
        }

        try {
            String deviceType = reqParamMap.get("deviceType");
            List<AppVersionControlVo> resultList = new ArrayList<>();
            if("ios".equals(deviceType)){
                resultList = appVersionControlService.findAllByPlatformNot(2);
            }else if("android".equals(deviceType)){
                resultList = appVersionControlService.findAllByPlatformNot(1);
            }
            return ResponseResult.buildResult(ResultEnum.GET_VERSION_SUCCESS, resultList);
        } catch (Exception e) {
            log.error("【获取版本列表信息出现异常】:{}", ExceptionUtil.logExceptionStack(e));
            throw new CustomException(ResultEnum.GET_VERSION_EXCEPTION);
        }
    }
}
